using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 内容模型及内容项管理接口。
    /// 提供内容模型、字段、内容项的增删改查及状态管理等 API。
    /// </summary>
    [ApiController]
    [Route("api/content")]
    public class ContentController : ControllerBase
    {
        /// <summary>
        /// 内容服务应用层接口
        /// </summary>
        private readonly IContentAppService _service;

        /// <summary>
        /// 构造函数，注入内容服务
        /// </summary>
        /// <param name="service">内容服务应用层接口</param>
        public ContentController(IContentAppService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有内容模型（需登录）
        /// </summary>
        /// <returns>内容模型列表</returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetAllModels()
            => Ok(await _service.GetAllModelsAsync());

        /// <summary>
        /// 创建内容模型（需登录）
        /// </summary>
        /// <param name="dto">内容模型数据传输对象</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> CreateModel([FromBody] ContentModelDto dto)
        {
            await _service.CreateModelAsync(dto);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 根据ID获取内容模型（需登录）
        /// </summary>
        /// <param name="id">内容模型主键</param>
        /// <returns>内容模型数据</returns>
        [Authorize]
        [HttpGet("{id}")]
        public async Task<IActionResult> GetModelById(long id)
            => Ok(await _service.GetModelByIdAsync(id));

        /// <summary>
        /// 更新内容模型（需登录）
        /// </summary>
        /// <param name="id">内容模型主键</param>
        /// <param name="dto">内容模型数据传输对象</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateModel(long id, [FromBody] ContentModelDto dto)
        {
            await _service.UpdateModelAsync(id, dto);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 删除内容模型（需管理员）
        /// </summary>
        /// <param name="id">内容模型主键</param>
        /// <returns>操作结果</returns>
        [Authorize(Roles = "Admin")]
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteModel(long id)
        {
            await _service.DeleteModelAsync(id);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 获取模型字段（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <returns>字段列表</returns>
        [Authorize]
        [HttpGet("{modelId}/fields")]
        public async Task<IActionResult> GetFields(long modelId)
            => Ok(await _service.GetFieldsAsync(modelId));

        /// <summary>
        /// 添加模型字段（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="dto">字段数据传输对象</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPost("{modelId}/fields")]
        public async Task<IActionResult> AddField(long modelId, [FromBody] FieldDto dto)
        {
            await _service.AddFieldAsync(modelId, dto);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 更新模型字段（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="fieldId">字段主键</param>
        /// <param name="dto">字段数据传输对象</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPut("{modelId}/fields/{fieldId}")]
        public async Task<IActionResult> UpdateField(long modelId, long fieldId, [FromBody] FieldDto dto)
        {
            await _service.UpdateFieldAsync(modelId, fieldId, dto);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 删除模型字段（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="fieldId">字段主键</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpDelete("{modelId}/fields/{fieldId}")]
        public async Task<IActionResult> DeleteField(long modelId, long fieldId)
        {
            await _service.DeleteFieldAsync(modelId, fieldId);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 获取内容项列表（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <returns>内容项列表</returns>
        [Authorize]
        [HttpGet("{modelId}/items")]
        public async Task<IActionResult> GetItems(long modelId, int page = 1, int limit = 10)
            => Ok(await _service.GetItemsAsync(modelId, page, limit));

        /// <summary>
        /// 创建内容项（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="dto">内容项数据传输对象</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPost("{modelId}/items")]
        public async Task<IActionResult> CreateItem(long modelId, [FromBody] ContentItemDto dto)
        {
            await _service.CreateItemAsync(modelId, dto);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 根据ID获取内容项（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="itemId">内容项主键</param>
        /// <returns>内容项数据</returns>
        [Authorize]
        [HttpGet("{modelId}/items/{itemId}")]
        public async Task<IActionResult> GetItemById(long modelId, long itemId)
            => Ok(await _service.GetItemByIdAsync(modelId, itemId));

        /// <summary>
        /// 更新内容项（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="itemId">内容项主键</param>
        /// <param name="dto">内容项数据传输对象</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPut("{modelId}/items/{itemId}")]
        public async Task<IActionResult> UpdateItem(long modelId, long itemId, [FromBody] ContentItemDto dto)
        {
            await _service.UpdateItemAsync(modelId, itemId, dto);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 删除内容项（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="itemId">内容项主键</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpDelete("{modelId}/items/{itemId}")]
        public async Task<IActionResult> DeleteItem(long modelId, long itemId)
        {
            await _service.DeleteItemAsync(modelId, itemId);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 变更内容项状态（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="itemId">内容项主键</param>
        /// <param name="status">新状态</param>
        /// <returns>操作结果</returns>
        [Authorize]
        [HttpPatch("{modelId}/items/{itemId}/status")]
        public async Task<IActionResult> ChangeItemStatus(long modelId, long itemId, [FromBody] string status)
        {
            await _service.ChangeItemStatusAsync(modelId, itemId, status);
            return Ok(new { success = true });
        }

        /// <summary>
        /// 获取内容项状态（需登录）
        /// </summary>
        /// <param name="modelId">内容模型主键</param>
        /// <param name="itemId">内容项主键</param>
        /// <returns>内容项状态</returns>
        [Authorize]
        [HttpGet("{modelId}/items/{itemId}/status")]
        public async Task<IActionResult> GetItemStatus(long modelId, long itemId)
            => Ok(await _service.GetItemStatusAsync(modelId, itemId));
    }
}
